package com.iraeta.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iraeta.Config;
import com.iraeta.common.ServiceException;
import com.iraeta.entity.main.erpinterface.ItemMaster;
import com.iraeta.uitl.OkHttpClientRest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ErpU9_Interface {
    private static final Logger logger = LoggerFactory.getLogger(ErpU9_Interface.class);

    @Autowired
    Config config;

    public String getToken(String userCode){
        final String entCode = "002";
        final String orgCode = "004";
        Map<String, String> querys = new HashMap<>();
        querys.put("clientid", config.clientId);
        querys.put("clientsecret", config.clientSecret);
        querys.put("entCode", entCode);
        querys.put("orgCode", orgCode);
        querys.put("userCode", userCode);
        try {
            String res = OkHttpClientRest.doGet(config.erpHost + "/webapi/OAuth2/AuthLogin", null, null, querys);
            JSONObject tokenJb = JSON.parseObject(res);
            if(!tokenJb.getBoolean("Success")){
                throw new ServiceException(tokenJb.getString("ResMsg"));
            }
            return tokenJb.getString("Data");
        }catch (Exception e){
            logger.error("获取token失败1 : [" + e.getMessage() + "]");
            throw new ServiceException("获取token失败1 : [" + e.getMessage() + "]");
        }
    }

    public String getCunHuo(String userCode, String code){
        String token = this.getToken(userCode);
        Map<String,String> headerMap = new HashMap<>();
        headerMap.put("Content-Type", "application/json");
        headerMap.put("token", token);

        List<Map<String,Object>> paramMapList = new ArrayList<>();
        Map<String,Object> paramMap1 = new HashMap<>();
        ItemMaster itemMaster = new ItemMaster();
        itemMaster.setID(0L);
        itemMaster.setName("");
        itemMaster.setCode(code);
        paramMap1.put("itemMaster", itemMaster);
        paramMap1.put("OtherID", "");
        paramMapList.add(paramMap1);
        String paramStr = JSON.toJSONString(paramMapList);
        try {
            String res = OkHttpClientRest.doPost(config.erpHost + "/webapi/ItemMaster/Query", headerMap, paramStr);
            JSONObject resJb = JSON.parseObject(res);
            if(!resJb.getBoolean("Success")){
                throw new ServiceException(resJb.getString("ResMsg"));
            }
            return resJb.getString("Data");
        }catch (Exception e){
            logger.error("获取存货失败 : [" + e.getMessage() + "]");
            throw new ServiceException("获取存货失败 : [" + e.getMessage() + "]");
        }
    }

    public String addCaiGou(String userCode, String caiGouBody){
        String token = this.getToken(userCode);
        Map<String,String> headerMap = new HashMap<>();
        headerMap.put("Content-Type", "application/json");
        headerMap.put("token", token);
        try {
            String res = OkHttpClientRest.doPost(config.erpHost + "/webapi/PurchaseOrder/Create", headerMap, caiGouBody);
            JSONObject resJb = JSON.parseObject(res);
            if(!resJb.getBoolean("Success")){
                throw new ServiceException(resJb.getString("ResMsg"));
            }
            JSONArray dataJA = resJb.getJSONArray("Data");
            JSONObject dataItemJb = dataJA.getJSONObject(0);
            if(!dataItemJb.getBoolean("IsSucess")){
                throw new ServiceException("行接口失败:" + dataItemJb.getString("ErrorMsg"));
            }
            return dataItemJb.getString("Code") + "|" + dataItemJb.getString("ID") ;
        }catch (Exception e){
            logger.error("调用采购订单接口失败 : [" + e.getMessage() + "]");
            throw new ServiceException("调用采购订单接口失败 : [" + e.getMessage() + "]");
        }
    }
}
